home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / NotificationMon ƒ / NotificationMon.ƒ / Source / NotificationWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-18  |  6.1 KB  |  273 lines  |  [TEXT/KAHL]

  1. #include "ScrollBar.h"
  2. #include "NotificationMon.h"
  3. #include "Event.h"
  4.  
  5.  NMRec            Notification;
  6.  Boolean        NotificationPending = false;
  7.  
  8. void UpdateScrollMax(notificationDoc *nmd)
  9. {
  10.     short    pageSize;
  11.     
  12.     pageSize = RECT_HEIGHT((WINDOW(nmd).portRect)) / nmd->lineHt;
  13.     if(nmd->ncount > pageSize)
  14.         SetCtlMax(nmd->vScroll, nmd->ncount - pageSize);
  15.     else
  16.         SetCtlMax(nmd->vScroll, nmd->ncount);
  17. }
  18.  
  19.  
  20. void InitNotificationWindow(notificationDoc *nmd)
  21. {
  22.  
  23.     nmd->vScroll =  NewVScrollBar(&nmd->nWin);
  24.     GetWindowGlobalRect(&nmd->nWin.port, 
  25.                     &nmd->nWin.port.portRect, 
  26.                     &nmd->prefs.windowLoc);
  27.  
  28.     if(!RectInRgn(&nmd->prefs.windowLoc,GetGrayRgn()))
  29.         MoveWindow(&nmd->nWin.port,4,40,false);
  30. }
  31.  
  32. void SizeNotificationWindow(notificationDoc *nmd, EventRecord *evt)
  33. {
  34.     RgnHandle    desk;
  35.     
  36.     long    newSize;
  37.     Rect    sizeRect;
  38.     GrafPtr    savePort;
  39.     
  40.     GetPort(&savePort);
  41.     SetPort(&WINDOW(nmd));
  42.  
  43.     desk = GetGrayRgn();
  44.     
  45.     sizeRect.top = nmd->lineHt * 4;                        /* min height */
  46.     sizeRect.bottom = sizeRect.top + nmd->lineHt * nmd->ncount;            /* max height */
  47.  
  48.     sizeRect.left = 128;                    /* min window width */
  49.     sizeRect.right = sizeRect.left + (**desk).rgnBBox.right;
  50.     
  51.     newSize = GrowWindow(&WINDOW(nmd),evt->where,&sizeRect);
  52.     SizeWindow(&WINDOW(nmd),LoWord(newSize),HiWord(newSize),false);
  53.     SizeVScrollBar(nmd->vScroll, &WINDOW(nmd));
  54.     UpdateScrollMax(nmd);
  55.     InvalRect(&WINDOW(nmd).portRect);
  56.     SetPort(savePort);
  57. }
  58.  
  59. void ZoomNotificationWindow(notificationDoc *nmd, short part)
  60. {
  61.     WStateData    **wState;
  62.     FontInfo    fi;
  63.     GrafPtr        savePort;
  64.     RgnHandle    desk;
  65.     
  66.     desk = GetGrayRgn();
  67.     
  68.     GetPort(&savePort);
  69.     SetPort(&nmd->nWin);
  70.     
  71.     wState = nmd->nWin.dataHandle;
  72.  
  73.     (**wState).stdState.right = (**wState).stdState.left 
  74.                     + nmd->longestLine;
  75.  
  76.     if(nmd->ncount > 3)
  77.         (**wState).stdState.bottom = (**wState).stdState.top
  78.                     + (nmd->lineHt * nmd->ncount);
  79.     else
  80.         (**wState).stdState.bottom = (**wState).stdState.top
  81.                     + (nmd->lineHt * 4);
  82.  
  83.     if((**wState).stdState.bottom > (**desk).rgnBBox.bottom)
  84.         (**wState).stdState.bottom = (**desk).rgnBBox.bottom; 
  85.     
  86.     
  87.     if((**wState).stdState.right > (**desk).rgnBBox.right)
  88.         (**wState).stdState.right = (**desk).rgnBBox.right;
  89.     
  90.     ZoomWindow(&nmd->nWin,part,true);
  91.     UpdateScrollMax(nmd);
  92.     SizeVScrollBar(nmd->vScroll, &nmd->nWin);
  93.  
  94.     SetPort(savePort);
  95. }
  96.  
  97. void ActivateNotificationWindow(notificationDoc *nmd)
  98. {
  99.     ShowControl(nmd->vScroll);
  100. }
  101.  
  102.  
  103. void DeactivateNotificationWindow(notificationDoc *nmd)
  104. {
  105.     HideControl(nmd->vScroll);
  106. }
  107.  
  108.  
  109. void NotificationCheckScrollVis()
  110. {
  111.     if(InBackGround)
  112.         HiliteControl(MainView->vScroll,255);
  113.     else if( (MainView->ncount * MainView->lineHt) > RECT_HEIGHT(MainView->nWin.port.portRect) )
  114.             HiliteControl(MainView->vScroll,0);
  115.         else
  116.             HiliteControl(MainView->vScroll,255);
  117. }
  118.  
  119. void NotificationDocLayerSwitch()
  120. {
  121.  
  122.     NotificationCheckScrollVis();
  123.  
  124.     if(!InBackGround && NotificationPending)
  125.         NMRemove(&Notification);
  126.  
  127.     if(MainView->prefs.hideWindowInBackground)
  128.         if(InBackGround)
  129.             HideWindow(&MainView->nWin);
  130.         else
  131.             ShowWindow(&MainView->nWin);
  132. }
  133.  
  134. void SelectNotification(WindowPtr wp, Point pt)
  135. {
  136.     Rect            hitRect;
  137.     notificationDoc *nmd;    
  138.     short            i;
  139.     GrafPtr            savePort;
  140.     Boolean            hitOne = false;
  141.     long            postTick;
  142.     
  143.     GetPort(&savePort);
  144.     SetPort(wp);
  145.  
  146.     nmd = (notificationDoc *)wp;
  147.  
  148.  
  149.     hitRect = nmd->nWin.port.portRect;
  150.     hitRect.right -= 16;
  151.     hitRect.bottom = hitRect.top + nmd->lineHt;
  152.     
  153.     ClipRect(&hitRect);
  154.     PenSize(4,4);
  155.     MoveTo(pt.h,pt.v);
  156.     LineTo(pt.h,pt.v);
  157.     PenMode(patXor);
  158.  
  159.     for(i=1; i <= nmd->ncount; i++) {
  160.         FrameRect(&hitRect);
  161.         if(PtInRect(pt, &hitRect)) {
  162.             hitOne = true;
  163.             if( i == nmd->nSelection) {
  164.                 /* we bring the posting application forward */
  165.             } else {
  166.                 InvalRect(&nmd->nWin.port.portRect);            /* update for new selection */
  167.             /*
  168.                 if(nmd->nSelection) {
  169.                     hitRect = nmd->nWin.port.portRect;
  170.                     hitRect.right -= 16;
  171.                     hitRect.bottom = hitRect.top + nmd->lineHt;
  172.                     if(nmd->nSelection > 1)
  173.                         OffsetRect(&hitRect,0,nmd->lineHt * nmd->nSelection);
  174.                 }
  175.             */
  176.                 nmd->nSelection = i;
  177.             }
  178.         }
  179.         FrameRect(&hitRect);
  180.         Delay(30,&postTick);
  181.         OffsetRect(&hitRect, 0, nmd->lineHt);
  182.     }
  183.     
  184.     if(!hitOne && nmd->nSelection) {
  185.         hitRect = nmd->nWin.port.portRect;
  186.         hitRect.right -= 16;
  187.         hitRect.bottom = hitRect.top + nmd->lineHt;
  188.         if(nmd->nSelection > 1)
  189.             OffsetRect(&hitRect,0,nmd->lineHt * nmd->nSelection);
  190.         InvalRect(&hitRect);
  191.         nmd->nSelection = 0;
  192.     }
  193.     
  194.     PenNormal();
  195.     SetPort(savePort);
  196. }
  197.  
  198. void PostNotificationIcon()
  199. {
  200.  
  201.     if(!MainView->prefs.notifyNewMessage) return;
  202.     if(NotificationPending)    return;
  203.     
  204.     Notification.qType = nmType;
  205.     Notification.nmMark = 1;
  206.     Notification.nmIcon = GetResource('SICN',128);
  207.     Notification.nmSound = 0;
  208.     Notification.nmStr = nil;
  209.     Notification.nmResp = nil;
  210.     Notification.nmRefCon = 'BRIG';    
  211.  
  212.     NMInstall(&Notification);    
  213.     NotificationPending = true;
  214. }
  215.  
  216. pascal void NotificationScrollProc(ControlHandle ctl, short part)
  217. {
  218.     WindowPtr         wp;
  219.     short            scrollAmt = 0, pageSize = 0, prevValue;
  220.     RgnHandle        scrollRgn;
  221.     Rect            scroll;
  222.     
  223.     pageSize = (RECT_HEIGHT(MainView->nWin.port.portRect) / MainView->lineHt) -1;
  224.     
  225.     switch(part) {
  226.         case inUpButton:    scrollAmt = -1;
  227.                             break;
  228.         case inDownButton:    scrollAmt = 1;
  229.                             break;
  230.         case inPageUp:        scrollAmt = -pageSize;
  231.                             break;
  232.         case inPageDown:    scrollAmt = pageSize;
  233.                             break;
  234.     }
  235.     
  236.     if(scrollAmt) {
  237.         prevValue = GetCtlValue(ctl);
  238.         SetCtlValue(ctl, prevValue + scrollAmt);
  239.         if(prevValue != GetCtlValue(ctl)) {
  240.             scrollRgn = NewRgn();
  241.             scroll = WINDOW(MainView).portRect;
  242.             scroll.right -= 16;
  243.             ScrollRect(&scroll, 0 , -(scrollAmt * MainView->lineHt), scrollRgn);
  244.             InvalRgn(scrollRgn);
  245.             BeginUpdate(&WINDOW(MainView));
  246.             UpdateNotifications(MainView,GetCtlValue(ctl));
  247.             EndUpdate(&WINDOW(MainView));
  248.             DisposeRgn(scrollRgn);
  249.         }
  250.     }
  251. }
  252.  
  253. void NotificationClick(WindowPtr wp, EventRecord *event)
  254. {
  255.         ControlHandle    whichControl;
  256.         Point            pt;
  257.         short            part;
  258.         
  259.         pt = event->where;
  260.         GlobalToLocal(&pt);
  261.         if(part = FindControl(pt,wp,&whichControl))
  262.         {
  263.             if(part == inThumb) {
  264.                 TrackControl(whichControl,pt,nil);
  265.                 UpdateNotifications(wp,GetCtlValue(whichControl));
  266.             }
  267.             else
  268.                 TrackControl(whichControl,pt,NotificationScrollProc);
  269.         } else {
  270.             SelectNotification(wp,pt);
  271.         }
  272. }
  273.